package com.practicaljava.lesson11;

import java.awt.Component;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;

public class TicTacToeGameApplet extends JApplet implements MouseListener
{
    private static final String PLAYERX = "Player X";
    private static final String PLAYERO = "Player O";
    
    private String playerName = PLAYERX;
    
    private static final TicTacToePlayer computer = new TicTacToePlayer();
    
    private JButton buttons[] = new JButton[9];
    private JButton playAgain;
    
    private javax.swing.JLabel playerNumber;
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JPanel controlsPanel;
    private javax.swing.JPanel bottomPanel;

    public void init(){
        initComponents();
        PlayStrategy randomMove = new RandomMove();
        computer.setStrategy(randomMove);
    }

    private void initComponents(){
        buttonsPanel = new javax.swing.JPanel();
        buttonsPanel.setLayout(new java.awt.GridLayout(4, 3));
        controlsPanel = new javax.swing.JPanel();
        controlsPanel.setLayout(new javax.swing.BoxLayout(controlsPanel, BoxLayout.Y_AXIS));
        bottomPanel = new javax.swing.JPanel();
        bottomPanel.setLayout(new javax.swing.BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
        
        for(int i=0; i<buttons.length; i++){
            buttons[i] = new JButton();
        }
        
        Font labelFont = new Font("Times New Roman", Font.PLAIN, 18);
        playerNumber = new javax.swing.JLabel();
        playerNumber.setFont(labelFont);
        setPlayerName(PLAYERX);
        controlsPanel.add(playerNumber);
               
        Font buttonFont1 = new Font("Times New Roman", Font.PLAIN, 60);
        Font buttonFont2 = new Font("Times New Roman", Font.PLAIN, 20);
        playAgain = new JButton();
        playAgain.setText("Play Again");
        playAgain.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                reset();
            }
        });
        playAgain.setFont(buttonFont2);
        controlsPanel.add(playAgain);
                     
        for(JButton b:buttons){
            b.addMouseListener(this);
            b.setFont(buttonFont1);
            b.setBackground(Color.LIGHT_GRAY);
            buttonsPanel.add(b);
        }
                   
        bottomPanel.add(buttonsPanel);
        bottomPanel.add(controlsPanel);
        add(bottomPanel);
        this.setSize(350,400);
    }
    
    private void setPlayerName(String playerName){
        this.playerName = playerName;
//        playerNumber.setText(playerName  + " your turn. ");
    }
    
    private void reset(){

        for(JButton b:buttons){
            b.setText("");
            b.setBackground(Color.LIGHT_GRAY);
        }
        setPlayerName(PLAYERX);
        playerNumber.setText("");
        computer.reset();
    }
    
   public boolean checkForWinner(){
       
        if(findThreeInARowAndHighLight()){
            String winnerName=(playerName == PLAYERX)?PLAYERX:PLAYERO;        
            playerNumber.setText(winnerName.concat(" won. Congratulations!"));
            return true;
        }
        
        return false;
        
    }
   
    
    public void mouseClicked(MouseEvent e) {
        int m=0;
        javax.swing.JButton currentButton = (JButton)e.getComponent();
        if (currentButton.getText() == ""){               
                currentButton.setText("X");
                setPlayerName(PLAYERX);                
                for(int i=0; i<9; i++){
                computer.setPosition(i,buttons[i].getText());
                }                
                if (!checkForWinner()){
                    m = computer.move();               
                if (m<9) {
                    buttons[m].setText("O");
                    setPlayerName(PLAYERO);
                    checkForWinner();
                    computer.setPosition(m, "O");
                }}
                                
        }
//        checkForWinner();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
    
    private boolean findThreeInARow(){
 
        if ((buttons[0].getText() == buttons[1].getText() && buttons[1].getText() == buttons[2].getText() && buttons[0].getText() != "") ||
                (buttons[3].getText() == buttons[4].getText() && buttons[4].getText() == buttons[5].getText() && buttons[3].getText() != "") ||
                (buttons[6].getText() == buttons[7].getText() && buttons[7].getText() == buttons[8].getText() && buttons[6].getText() != "") ||
                (buttons[0].getText() == buttons[3].getText() && buttons[3].getText() == buttons[6].getText() && buttons[0].getText() != "") ||
                (buttons[1].getText() == buttons[4].getText() && buttons[4].getText() == buttons[7].getText() && buttons[1].getText() != "") ||
                (buttons[2].getText() == buttons[5].getText() && buttons[5].getText() == buttons[8].getText() && buttons[2].getText() != "") ||
                (buttons[0].getText() == buttons[4].getText() && buttons[4].getText() == buttons[8].getText() && buttons[0].getText() != "") ||
                (buttons[2].getText() == buttons[4].getText() && buttons[4].getText() == buttons[6].getText() && buttons[2].getText() != "")
            )
            return true;
        else
            return false;
    }
    
    private boolean findThreeInARowAndHighLight(){
         
        if (buttons[0].getText() == buttons[1].getText() && buttons[1].getText() == buttons[2].getText() && buttons[0].getText() != ""){
            buttons[0].setBackground(Color.ORANGE);
            buttons[1].setBackground(Color.ORANGE);
            buttons[2].setBackground(Color.ORANGE);
            return true;    
        }
        if (buttons[3].getText() == buttons[4].getText() && buttons[4].getText() == buttons[5].getText() && buttons[3].getText() != ""){
            buttons[3].setBackground(Color.ORANGE);
            buttons[4].setBackground(Color.ORANGE);
            buttons[5].setBackground(Color.ORANGE);
            return true;
        }
        if (buttons[6].getText() == buttons[7].getText() && buttons[7].getText() == buttons[8].getText() && buttons[6].getText() != ""){
            buttons[6].setBackground(Color.ORANGE);
            buttons[7].setBackground(Color.ORANGE);
            buttons[8].setBackground(Color.ORANGE);
            return true;
        }
        if (buttons[0].getText() == buttons[3].getText() && buttons[3].getText() == buttons[6].getText() && buttons[0].getText() != ""){
            buttons[0].setBackground(Color.PINK);
            buttons[3].setBackground(Color.PINK);
            buttons[6].setBackground(Color.PINK);
             return true;      
        }
        if (buttons[1].getText() == buttons[4].getText() && buttons[4].getText() == buttons[7].getText() && buttons[1].getText() != ""){
            buttons[1].setBackground(Color.PINK);
            buttons[4].setBackground(Color.PINK);
            buttons[7].setBackground(Color.PINK);
            return true;       
        }
        if (buttons[2].getText() == buttons[5].getText() && buttons[5].getText() == buttons[8].getText() && buttons[2].getText() != ""){
            buttons[2].setBackground(Color.GREEN);
            buttons[5].setBackground(Color.GREEN);
            buttons[8].setBackground(Color.GREEN);
            return true;      
        }
        if (buttons[0].getText() == buttons[4].getText() && buttons[4].getText() == buttons[8].getText() && buttons[0].getText() != ""){
            buttons[0].setBackground(Color.GREEN);
            buttons[4].setBackground(Color.GREEN);
            buttons[8].setBackground(Color.GREEN);
            return true;       
        }
        if (buttons[2].getText() == buttons[4].getText() && buttons[4].getText() == buttons[6].getText() && buttons[2].getText() != ""){
            buttons[2].setBackground(Color.MAGENTA);
            buttons[4].setBackground(Color.MAGENTA);
            buttons[6].setBackground(Color.MAGENTA);
            return true;      
                }
          
       return false;
    }
}